# C++ code generator for protocol command function
# author: ened
# date: 2013-09-30

import re

protocol_file = "protocol.h"
output_file = "helper_cmd.cpp"

lines = [line.strip() for line in open(protocol_file)]


pre_str = "#include \"protocol.h\"\n" \
"\n" \
"#define CASE(val)	case val: strcpy(str, #val + 4); break;\n" \
"\n" \
"namespace ProjectZ {\n" \
"\n" \
"const char* GetCommandName(char str[80], unsigned short command)\n" \
"{\n" \
"	switch (command)\n" \
"	{\n"

post_str = "	default:\n" \
"		sprintf(str, \"0x%x\", command);\n" \
"	}\n" \
"	return str;\n" \
"}\n" \
"\n" \
"}\n"


o = open(output_file, 'w+')
o.write(pre_str)

for l in lines:
	d = re.split(r'[\t\n ]+', l)
	if d[0] == '#define' and d[1][:4] == "CMD_":
		o.write( "\tCASE( {0} )\n".format(d[1]) )
		
o.write(post_str)
o.close()
